package com.example.service.impl;

import com.example.common.result.Result;
import com.example.entity.mes.Unit;
import com.example.mapper.UnitMapper;
import com.example.service.UnitService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 14501
* @description 针对表【unit】的数据库操作Service实现
* @createDate 2024-03-04 14:48:29
*/
@Service
@Slf4j
public class UnitServiceImpl extends ServiceImpl<UnitMapper, Unit> implements UnitService{

    @Resource
    private UnitMapper unitMapper;

    @Override
    public Result queryUnit(Long id, String name, String status) {
       try {
           QueryWrapper<Unit> unitQueryWrapper = new QueryWrapper<>();
           if (name!=null && !name.equals("")){
               unitQueryWrapper.like("name",name);
           }
           if (status!=null && !status.equals("")){
               unitQueryWrapper.eq("status",status);
               List<Unit> units = this.unitMapper.selectList(unitQueryWrapper);
               return Result.success(units);
           }
           List<Unit> units = this.unitMapper.selectList(unitQueryWrapper);
           if (units==null){
               return Result.error("暂无数据");
           }
           return Result.success(setSubMenu(units));
       }catch (Exception e){
           return Result.error(String.valueOf(e+"查询发生异常"),"500");
       }
    }


    /**
     * 查询父类和父下面的子类
     * @param list
     * @return
     */
    public List<Unit> setSubMenu(List<Unit> list) {
        // 父级菜单
        List<Unit> parentList = list.stream().parallel()
                .filter(menu -> menu.getParentId() == 0).collect(Collectors.toList());
       if (parentList.size()>0){
           for (Unit parentMenu : list) {
               // 子菜单
               List<Unit> subList = list.stream().parallel()
                       .filter(menu -> menu.getParentId() == parentMenu.getId()).collect(Collectors.toList());
               parentMenu.setChildren(subList);
           }
           return parentList;
       }
       return list;
    }

    @Override
    public Result queryUnitById(Long id) {
        try {
            Unit unit = this.unitMapper.selectById(id);
            if (unit==null){
                return Result.error("暂无数据","500");
            }
            return Result.success(unit);
        }catch (Exception e){
            return Result.error(String.valueOf(e+"选择的id不存在,发生异常"),"500");
        }
    }

    @Override
    public Result deleteUnit(Long id) {
        try {
            int i = this.unitMapper.deleteById(id);
            if (i>0){
                return Result.success();
            }else {
                return Result.error("删除失败");
            }
        }catch (Exception e){
            return Result.error(String.valueOf(e+"选择的id不存在,删除发生异常"),"500");
        }
    }

    @Override
    public Result updateUnit(Unit unit) {
        try {
            int i = this.unitMapper.updateById(unit);
            if (i>0){
                return Result.success();
            } else {
                return Result.error("修改失败");
            }
        }catch (Exception e){
            return Result.error(String.valueOf(e+"修改发生异常"),"500");
        }
    }

    @Override
    public Result insertUnit(Unit unit) {
        try {
            unit.setCode(SerialNumber());
            int insert = this.unitMapper.insert(unit);
            if (insert>0){
                return Result.success();
            }else {
                return Result.error("新增失败");
            }
        }catch (Exception e){
            return Result.error(String.valueOf(e+"新增发生异常"),"500");
        }
    }

    @Override
    public Result queryUnitList(String name) {
        try {
            QueryWrapper<Unit> unitQueryWrapper = new QueryWrapper<>();
            if (name!=null && !name.equals("")){
                unitQueryWrapper.like("name",name);
                List<Unit> list = this.unitMapper.selectList(unitQueryWrapper);
                List<Unit> unitList =new ArrayList<>();
                List<Unit> unitLists =new ArrayList<>();
                for (Unit arr:list) {
                    if (arr.getParentId()!=0){
                        Unit unit = this.unitMapper.selectById(arr.getParentId());
                        if (arr.getParentId() == unit.getId()){
                            unitList.add(arr);
                        }
                        unit.setChildren(unitList);
                        unitLists.add(unit);
                    }else {
                        unitLists.add(arr);
                    }
                }
                return Result.success(unitLists.stream().parallel().distinct().collect(Collectors.toList()));
            }
            List<Unit> list = this.unitMapper.selectList(unitQueryWrapper);
            if (list.size()==0){
                return Result.error("暂无数据","500");
            }
            return Result.success(setSubMenu(list));
        }catch (Exception e){
            return Result.error(String.valueOf(e+"查询发生异常"),"500");
        }
    }


    //获取流水号
    public String SerialNumber() {
        QueryWrapper<Unit> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("code");
        //查询最新的流水号
        List<Unit> sCells = unitMapper.selectList(queryWrapper);
        //用于保存获取最新的流水号
        String temp="";
        //用于拼接需要的流水号
        String serialNumber="";
        //保存最新的流水号
        if(sCells.size()>0){
            temp=sCells.get(0).getCode();
        }
        //时间字符串生成
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        //当前日期
        String date = format.format(new Date());
        //获取当前年份
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy");
        //当前年份
        String date2 = format2.format(new Date());
        //判断数据库是否有数据并且是处于当前年份
        if (temp!=""){
            Integer aa=Integer.parseInt(temp)+1;
            serialNumber=String.valueOf(aa);
        }else{
            serialNumber=date+temp+1;
        }
        return serialNumber;
    }
}




